"""
    @project_name: Fishing_Calendar
    @author：henry_oulen@163.com
    @file： formate_time.py.py
    @date：2023/3/21 18:49
"""

import time
import tkinter
from tkinter import ANCHOR, Text, RAISED, END
from tkinter.ttk import *

def cal2(event):
    if t1.get():
        time_local = time.localtime(int(t1.get()))  # 转化成localtime
        time_format = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
        comb3.delete('1.0', END)
        comb3.insert(END, str(time_format))


def cal3(event):
    if t2.get():
        formate_data = t2.get()
        if len(formate_data) == 10:
            # 转换成时间数组
            time_array = time.strptime(formate_data, "%Y-%m-%d")
            # 转换成时间戳
            time_format = int(time.mktime(time_array))
            comb3.delete('1.0', END)
            comb3.insert(END, str(time_format))
        elif len(formate_data) == 19:  # "2023-03-21 15:11:25"
            time_array = time.strptime(formate_data, "%Y-%m-%d %H:%M:%S")
            # 转换成时间戳
            time_format = int(time.mktime(time_array))
            comb3.delete('1.0', END)
            comb3.insert(END, str(time_format))


def listbox_copy(event):
    root.clipboard_clear()
    selected = comb3.get(ANCHOR)
    root.clipboard_append(selected)


def copy_to_clipboard():
    """Copy current contents of text_entry to clipboard."""
    root.clipboard_clear()  # Optional.
    root.clipboard_append(comb3.get('1.0', END).rstrip())

root = tkinter.Tk()
root.attributes("-topmost", True)
root.title('时间工具')
root.geometry('320x240')

lbl1 = Label(root, text='时间戳')
lbl1.place(relx=0.1, rely=0.1, relwidth=0.2, relheight=0.1)
t1 = Entry(root)
t1.place(relx=0.1, rely=0.2, relwidth=0.4, relheight=0.1)

lbl2 = Label(root, text='时间格式')
lbl2.place(relx=0.5, rely=0.1, relwidth=0.2, relheight=0.1)
t2 = Entry(root)
t2.place(relx=0.5, rely=0.2, relwidth=0.4, relheight=0.1)

comb1 = Button(root, text="时间戳-->格式化", command="cal2")
comb1.bind("<Button-1>", cal2)
comb1.place(relx=0.1, rely=0.3, relwidth=0.4, relheight=0.1)

comb2 = Button(root, text="时间格式-->时间戳", command="cal3")
comb2.bind("<Button-1>", cal3)
comb2.place(relx=0.5, rely=0.3, relwidth=0.4, relheight=0.1)

comb3 = Text(root, width=80, height=27, border=4, relief=RAISED, font=("等线", 16,))
comb3.place(relx=0.1, rely=0.6, relwidth=0.7, relheight=0.2)

clp = Button(root, text="复制到粘贴板",
             command=copy_to_clipboard)
clp.place(relx=0.1, rely=0.8, relwidth=0.7, relheight=0.1)

root.mainloop()
